﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;

namespace Rose.Helper
{
    public class FeelingHelpModel
    {
        private Database_RoseEntities db = new Database_RoseEntities();


        public bool AddFeelingToDatabase(string content, string feel, string selfId)
        {
            try
            {
                Feeling addModel = new Feeling()
                {
                    Id = Guid.NewGuid(),
                    CreateTime = DateTime.Now,
                    Feel = CheckDataHelpModel.CheckLength(feel, 10),
                    //Description = EncodeContent(CheckDataHelpModel.CheckLength(content,144)),
                    Description = CheckDataHelpModel.CheckLength(HttpContext.Current.Server.UrlDecode(content), 140),
                    UserId = selfId,
                    CommentCount = 0
                };
                db.AddToFeelings(addModel);

                UserInfo selfInfo = db.UserInfos.Where(c => c.Id == selfId).FirstOrDefault();
                if (selfInfo == null)
                    throw new ArgumentNullException();
                selfInfo.Score += int.Parse(Rose.ViewRes.Defaultresx.FeelingAddScore);
                new NewTrendHelpModel().AddFeelingTrend(addModel);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        public bool RemoveFeelingFromDatabase(string id, string selfId)
        {
            try
            {
                Guid guidId = new Guid(id);
                Feeling deleteModel = db.Feelings.Where(c => c.Id == guidId && c.UserId == selfId).FirstOrDefault();
                if (deleteModel == null)
                    return true;
                db.Feelings.DeleteObject(deleteModel);
                db.SaveChanges();
                return true;
            }
            catch
            { return false; }

        }


        #region 获取列表
        private List<FeelingModel> ChangeFeelingToFeelingModel(List<Feeling> feelingList)
        {
            List<FeelingModel> returnModel = new List<FeelingModel>();
            CheckHelpModel checkhelp = new CheckHelpModel();
            foreach (var feeling in feelingList)
            {
                FeelingModel addModel = new FeelingModel()
                {
                    CreateTime = feeling.CreateTime.ToString(),

                    Description = HttpContext.Current.Server.HtmlEncode(feeling.Description),
                    Feel = feeling.Feel,
                    Id = feeling.Id.ToString(),
                    CommentCount = feeling.CommentCount,
                    UserId=feeling.UserId
                    
                    //CommentCount=FeelingCommentHelpModel.GetCommentCount(feeling.Id.ToString())

                };
                string username = checkhelp.CheckUserInfo(addModel.UserId).Name;
                addModel.UserName = username;
                returnModel.Add(addModel);
            }
            return returnModel;
        }

        private List<Feeling> GetFeelingList(string selfId, string type, int currentpage, int pagesize)
        {
            if (type == null)
                return (from item in db.Feelings
                        where item.UserId == selfId
                        orderby item.CreateTime descending
                        select item
                            ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();
            else
                return (from item in db.Feelings
                        where item.UserId == selfId && item.Feel == type
                        orderby item.CreateTime descending
                        select item
                        ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();
        }

        private List<Feeling> GetFeelingList(string selfId, string type, int currentpage, int pagesize, DateTime minTime)
        {
            if (type == null)
                return (from item in db.Feelings
                        where item.UserId == selfId && item.CreateTime > minTime
                        orderby item.CreateTime descending
                        select item
                        ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();
            else
                return (from item in db.Feelings
                        where item.UserId == selfId && item.CreateTime > minTime && item.Feel == type
                        orderby item.CreateTime descending
                        select item
                        ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();

        }
        private List<Feeling> GetFeelingList(string selfId, string type, int currentpage, int pagesize, DateTime minTime, DateTime maxTime)
        {
            if (type == null)
                return (from item in db.Feelings
                        where item.UserId == selfId && item.CreateTime > minTime && item.CreateTime < maxTime
                        orderby item.CreateTime descending
                        select item
                       ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();
            else
                return (from item in db.Feelings
                        where item.UserId == selfId && item.CreateTime > minTime && item.CreateTime < maxTime && item.Feel == type
                        orderby item.CreateTime descending
                        select item
                       ).Skip((currentpage - 1) * pagesize).Take(pagesize).ToList();

        }

        public List<FeelingModel> GetFeelingModelList(string selfId, int currentpage, int pagesize)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, null, currentpage, pagesize));
        }

        public List<FeelingModel> GetFeelingModelList(string selfId, int currentpage, int pagesize, DateTime minTime)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, null, currentpage, pagesize, minTime));
        }

        public List<FeelingModel> GetFeelingModelList(string selfId, int currentpage, int pagesize, DateTime minTime, DateTime maxTime)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, null, currentpage, pagesize, minTime, maxTime));
        }

        public int GetFeelingCount(string selfId)
        {

            return GetFeelingList(selfId, null, 1, int.MaxValue).Count();
        }

        public int GetFeelingCount(string selfId, DateTime minTime)
        {
            return GetFeelingList(selfId, null, 1, int.MaxValue, minTime).Count();
        }

        public int GetFeelingCount(string selfId, DateTime minTime, DateTime maxTime)
        {
            return GetFeelingList(selfId, null, 1, int.MaxValue, minTime, maxTime).Count();
        }

        #endregion

        #region 统计心情数量

        private int GetTypeCount(string selfId, string type, List<Feeling> feelings)
        {
            int returnCount = 0;
            foreach (var feeling in feelings)
            {
                if (feeling.Feel == type)
                {
                    returnCount++;
                }
            }
            return returnCount;
        }

        #region  所有心情

        //public  int GetTypeFeelingAllCount(string selfId, string type)
        //{
        //    return GetTypeCount(selfId, type, GetFeelingList(selfId, null, 1, int.MaxValue));
        //}
        //public  int GetTypeFeelingAllCount(string selfId, string type, DateTime minTime)
        //{
        //    return GetTypeCount(selfId, type, GetFeelingList(selfId, null, 1, int.MaxValue, minTime));
        //}

        //public  int GetTypeFeelingAllCount(string selfId, string type, DateTime minTime, DateTime maxTime)
        //{
        //    return GetTypeCount(selfId, type, GetFeelingList(selfId, null, 1, int.MaxValue, minTime, maxTime));
        //}

        #endregion





        #region 局部心情
        public int GetTypeFeelingSomeCount(string selfId, string type)
        {
            return GetTypeCount(selfId, type, GetFeelingList(selfId, type, 1, int.MaxValue));
        }

        public int GetTypeFeelingSomeCount(string selfId, string type, DateTime minTime)
        {
            return GetTypeCount(selfId, type, GetFeelingList(selfId, type, 1, int.MaxValue, minTime));
        }

        public int GetTypeFeelingSomeCount(string selfId, string type, DateTime minTime, DateTime maxTime)
        {
            return GetTypeCount(selfId, type, GetFeelingList(selfId, type, 1, int.MaxValue, minTime, maxTime));
        }
        #endregion


        #endregion

        #region 获取某种心情的列表
        public List<FeelingModel> GetTypeFeelingModelList(string selfId, string type, int currentpage, int pagesize)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, type, currentpage, pagesize));
        }

        public List<FeelingModel> GetTypeFeelingModelList(string selfId, string type, int currentpage, int pagesize, DateTime minTime)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, type, currentpage, pagesize, minTime));
        }

        public List<FeelingModel> GetTypeFeelingModelList(string selfId, string type, int currentpage, int pagesize, DateTime minTime, DateTime maxTime)
        {
            return ChangeFeelingToFeelingModel(GetFeelingList(selfId, type, currentpage, pagesize, minTime, maxTime));
        }

        #endregion
    }


}